package com.hmall.api.config;

import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    //注册日志级别：none->basic->headers->full
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
    //feign请求拦截器
    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(feign.RequestTemplate template) {
                //获取线程变量中的用户Id
                Long userId = UserContext.getUser();
                //将用户Id添加到feign请求头中
                if(userId != null){
                    template.header("user-info", userId.toString());
                }
            }
        };
    }
}
